package com.yhny.lykj.yuhannongye.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.AppManager;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.app.UserConfig;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.LCLog;
import com.yhny.lykj.yuhannongye.tools.Tools;

public class PasswordActivity extends BaseActivity {
    private TextView topTv;
    private EditText phoneEt;
    private EditText codeEt;
    private EditText passwordEt;
    private EditText psdAgainEt;
    private Button codeBt;
    //验证码按钮计时器
    private TimeCount time;
    int activity_id = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        TextView textView = (TextView)findViewById(R.id.name_top_bar);
        textView.setText("修改密码");
        findViewById(R.id.left_group).setOnClickListener(this);
        findViewById(R.id.left_text).setOnClickListener(this);
        activity_id = getIntent().getIntExtra(PASSWORD_ACTIVITY,-1);
        topTv = (TextView)findViewById(R.id.name_top_bar);
        if (activity_id==UNKNOW_PASSWORD) {
            topTv.setText("忘记密码");
        }else{
            topTv.setText("修改密码");
        }
        findViewById(R.id.left_group).setOnClickListener(this);
        findViewById(R.id.left_text).setOnClickListener(this);
        phoneEt = (EditText)findViewById(R.id.forget_phone);
        codeEt = (EditText)findViewById(R.id.forget_code);
        passwordEt = (EditText)findViewById(R.id.forget_password);

        psdAgainEt = (EditText)findViewById(R.id.forget_password_again);
        if(activity_id==MODIFY_PASSWORD){
            //修改密码
            phoneEt.setText(UserConfig.getConfigUserMobile());
            phoneEt.setEnabled(false);
            psdAgainEt.setVisibility(View.GONE);
            passwordEt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            passwordEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            View view =  (View)findViewById(R.id.view);
            view.setVisibility(View.GONE);
        }
        codeBt = (Button) findViewById(R.id.forget_code_bt);
        codeBt.setOnClickListener(this);
        findViewById(R.id.forget_submit).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_text:
            case R.id.left_group:{
                finish();
                break;
            }
            case R.id.forget_code_bt:{
                //获取验证码
                //发送短信验证码
                String phone = phoneEt.getText().toString();
                if (phone.isEmpty()){
                    LSCToast.show(this,"请输入手机号");
                    return;
                }
                if (!Tools.isMobilePhoneNumber(phone)){
                    LSCToast.show(this,"请输入正确的手机号");
                    return;
                }
                showProgressDialog(R.string.text_loading);
                DataManager.getData(MODEL_PROCESS_1, NetHelper.User.sendSMS(phone), this);
                break;
            }
            case R.id.forget_submit:{
                //确认
                String phone = phoneEt.getText().toString();
                String code = codeEt.getText().toString();
                String password = passwordEt.getText().toString();
                String psdagain = psdAgainEt.getText().toString();
                if (phone.isEmpty()){
                    LSCToast.show(this,"请输入手机号");
                    return;
                }
                if (!Tools.isMobilePhoneNumber(phone)){
                    LSCToast.show(this,"请输入正确的手机号");
                    return;
                }
                if (code.isEmpty()){
                    LSCToast.show(this,"请输入验证码");
                    return;
                }

                if (password.isEmpty()){
                    LSCToast.show(this,"请输入密码");
                    return;
                }
                if (password.length()<6&&password.length()>32){
                    LSCToast.show(this,"请输入6-32位密码");
                    return;
                }
                if(activity_id!=MODIFY_PASSWORD){
                    if (psdagain.isEmpty()){
                        LSCToast.show(this,"请再次输入密码");
                        return;
                    }
                    if (!password.equals(psdagain)){
                        LSCToast.show(this,"两次密码输入的不一致");
                        return;
                    }
                }

                showProgressDialog(R.string.text_loading);
                if (activity_id==UNKNOW_PASSWORD) {
                    DataManager.getData(MODEL_PROCESS_0, NetHelper.User.unKnowPassword(phone,password,code), this);
                }else{
                    DataManager.getData(MODEL_PROCESS_0, NetHelper.User.modifyPassword(phone,password,password,code), this);
                }

                break;
            }
        }
        super.onClick(v);
    }

    private void outLogin(){
        //退出登录
        UserConfig.clearUser();
        //阿里推送退出
    }

    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)||model==null) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
            case MODEL_PROCESS_0:{
                BaseModel baseModel = (BaseModel)model;
                if (isDataEmpty(baseModel)){
                    LSCToast.show(this,"修改失败！");
                }
                if (baseModel.status==1){
                        AppManager.getAppManager().finishAllActivity();
                        Intent intent = new Intent(PasswordActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                    LSCToast.show(this,"修改成功！");
                }else{
                    LCLog.e("baseModel = "+baseModel.msg);
                    LSCToast.show(this,"修改失败！");
                }
                break;
            }
            case MODEL_PROCESS_1:{
                BaseModel baseModel = (BaseModel)model;
                if (baseModel==null||baseModel.status!=1){
                    LSCToast.show(this,"发送失败");
                }else {
                    //进行验证
                    time=new TimeCount(60000,1000);//参数依次为总时长，计时时间间隔
                    time.start();
                    LSCToast.show(this,"发送成功");
                }
                break;
            }
        }
        dismissProgressDialog();
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (time!=null)
        {
            time.cancel();
            time=null;
        }
    }

    class  TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程显示
        @Override
        public void onTick(long millisUntilFinished) {
            codeBt.setTextSize(14);
            codeBt.setText(millisUntilFinished / 1000 + "秒后重新发送");
            codeBt.setClickable(false);
        }
        //计时完成触发
        @Override
        public void onFinish() {
            codeBt.setText("获取验证码");
            codeBt.setClickable(true);
        }
    }
}
